# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=('libarchive' 'libarchive-devel' 'bsdcpio' 'bsdtar')
pkgver=3.1.2
pkgrel=5
pkgdesc="library that can create and read several streaming archive formats"
arch=('i686' 'x86_64')
url="http://libarchive.org/"
license=('BSD')
groups=('libraries')
depends=('gcc-libs' 'libbz2' 'libiconv' 'liblzma' 'liblzo2' 'libnettle' 'libxml2' 'zlib')
makedepends=('libbz2-devel' 'libiconv-devel' 'liblzma-devel' 'liblzo2-devel' 'libnettle-devel' 'libxml2-devel' 'zlib-devel')
options=('!strip' 'debug' 'libtool')
# provides=('libarchive.so')
source=("http://libarchive.org/downloads/$pkgname-$pkgver.tar.gz"
        '0001-mtree-fix-line-filename-length-calculation.patch'
        'libarchive-3.1.3-CVE-2013-0211_read_buffer_overflow.patch'
        'libarchive-3.1.2-acl.patch'
        'libarchive-3.1.2-msys2.patch'
        'libarchive-3.1.2-sparce-mtree.patch')
md5sums=('efad5a503f66329bb9d2f4308b5de98a'
         'fda89c145bbcd793a96b06b463ef6a72'
         '6954a585c309dc548d1f2b05f2f3cfcb'
         'a5c995661c62429ceff2c23ea322393b'
         'ddb5c19d97cf9521864ef2f00baacee4'
         'cb344a879b3c4550fe3faf86c3826f23')

prepare() {
  cd "$pkgname-$pkgver"

  # https://code.google.com/p/libarchive/issues/detail?id=301
  # upstream commit e65bf287f0133426b26611fe3e80b51267987106
  patch -Np1 -i "$srcdir/0001-mtree-fix-line-filename-length-calculation.patch"

  # https://code.google.com/p/libarchive/issues/detail?id=329
  patch -Np1 -i "$srcdir/libarchive-3.1.2-acl.patch"

  # CVE-2013-0211
  patch -Np1 -i "$srcdir/libarchive-3.1.3-CVE-2013-0211_read_buffer_overflow.patch"

  # upstream commit 977bf2a4 - improved mtree support
  patch -p1 -i $srcdir/libarchive-3.1.2-sparce-mtree.patch

  # msysize patch
  patch -Np1 -i "$srcdir/libarchive-3.1.2-msys2.patch"

  autoreconf -ivf
}

build() {
  cd "$pkgname-$pkgver"

  ./configure \
    --prefix=/usr \
    --enable-shared \
    --enable-static \
    --without-libiconv-prefix

  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "$pkgname-$pkgver"

  #make check
}

package_bsdcpio() {
  groups=('base')

  mkdir -p $pkgdir/usr/bin
  mkdir -p $pkgdir/usr/share/man/man1/
  cp -f ${srcdir}/dest/usr/bin/bsdcpio.exe $pkgdir/usr/bin/
  cp -f ${srcdir}/dest/usr/share/man/man1/bsdcpio.* $pkgdir/usr/share/man/man1/
}

package_bsdtar() {
  groups=('base')

  mkdir -p $pkgdir/usr/bin
  mkdir -p $pkgdir/usr/share/man/man1/
  cp -f ${srcdir}/dest/usr/bin/bsdtar.exe $pkgdir/usr/bin/
  cp -f ${srcdir}/dest/usr/share/man/man1/bsdtar.* $pkgdir/usr/share/man/man1/
}

package_libarchive() {
  mkdir -p $pkgdir/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll $pkgdir/usr/bin/

  install -D -m644 ${srcdir}/${pkgname}-${pkgver}/COPYING $pkgdir/usr/share/licenses/libarchive/COPYING
}

package_libarchive-devel() {
  groups=('development')
  options=('staticlibs')
  depends=("libarchive=${pkgver}" 'libbz2-devel' 'libiconv-devel' 'liblzma-devel' 'liblzo2-devel' 'libnettle-devel' 'libxml2-devel' 'zlib-devel')

  mkdir -p $pkgdir/usr
  cp -rf ${srcdir}/dest/usr/include $pkgdir/usr/
  cp -rf ${srcdir}/dest/usr/lib $pkgdir/usr/
}
